<script setup lang='ts'>
import { User, Lock } from '@element-plus/icons-vue';
import { reactive, ref } from 'vue';
import useUserStore from '../../store/modules/user';
import type { loginFormData } from '../../api/user/type';
import { useRouter, useRoute } from 'vue-router';
import { ElNotification } from 'element-plus';
import { getTime } from '../../utils/time';

let loginForm = reactive<loginFormData>({
  username: '',
  password: ''
})
let useUser = useUserStore()
let $router = useRouter()
let $route = useRoute()
let time = getTime()
let loginForms = ref<any>()

const validatorUserName = (_: any, value: any, callback: any) => {
  let reg = /^.{5,11}$/
  if (!value) {
    return callback(new Error('用户名不能为空'))
  }
  if (reg.test(value)) {
    callback()
  } else {
    callback(new Error('用户名至少为5位'))
  }
}

const validatorPassword = (_: any, value: any, callback: any) => {
  let reg = /^.{5,11}$/
  if (!value) {
    return callback(new Error('密码不能为空'))
  }
  if (reg.test(value)) {
    callback()
  } else {
    callback(new Error('密码至少为5位'))
  }
}
let rules = reactive<any>({
  username: [
    // {required:true, message:'用户名不能为空', trigger:'change'},
    // {min:5, message:'用户名至少为5位'}
    { trigger: 'change', validator: validatorUserName }
  ],
  password: [
    // {required:true, message:'密码不能为空', trigger:'change'},
    // {min:6, message:'密码至少为6位'}
    { trigger: 'change', validator: validatorPassword }
  ]
})

const login = async (data: loginFormData) => {
  await loginForms.value.validate()
  try {
    await useUser.userLogin(data)
    useUser.userInfo()
    let redirect = $route.query.redirect
    $router.push({ path: redirect || '/' })
    ElNotification.success({
      title: '欢迎回来!',
      message: `${time}好!`,
    })
  } catch (error) {
    ElNotification.error({
      message: '登陆失败!',
    })
  }
}
</script>


<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :rules="rules" :model="loginForm" ref="loginForms">
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <div class="form">
            <el-form-item prop="username">
              <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input :prefix-icon="Lock" v-model="loginForm.password" show-password></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" style="width: 100%;" @click="login(loginForm)">登录</el-button>
            </el-form-item>
          </div>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>


<style scoped lang='scss'>
.login_container {
  width: 100%;
  height: 100vh;
  background: url(../../assets/images/background.jpg) no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background-color: rgba($color: #282be9, $alpha: 0.6);

    padding: 40px;


    h1 {
      color: #fff;
      font-size: 40px;
    }

    h2 {
      color: #fff;
      font-size: 20px;
      margin: 20px 0;
    }

    .form {
      width: 80%;
      margin: auto;
    }
  }
}
</style>